//
//  DZQViewController.m
//  AnnCar_user
//
//  Created by LiBenCan on 17/10/27.
//  Copyright © 2017年 LiBenCan. All rights reserved.
//

#import "DZQViewController.h"
#import "DZQCell.h"
#import "ChooseKindCell.h"
@interface DZQViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableV;
@property (nonatomic,strong)NSMutableArray *dataArr;//数据
@property (nonatomic,strong)UIView *bynView;//按钮View
@property (nonatomic,strong)UILabel *lineLab;//划线的Lab
@property (nonatomic,strong)CanLab  *btnLab;//顶部按钮
@property (nonatomic,strong)CanLab *selectLab;//选中的Lab
@property (nonatomic,strong)CanLab *ChooseLab;//筛选的Lab
@property (nonatomic,assign)BOOL isChoose;//是否是筛选
@property (nonatomic,strong)NSArray *listArr;//假数据
@end

@implementation DZQViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initTitleWithTitle:@"我的电子券"];
    [self initLeftBarButtonWithImage:@"back"];
    _isChoose = NO;
    self.view.backgroundColor = backColor;
    [self addBtnView];
    [self ChooseLab];
    _listArr = @[@"折扣券",
                 @"现金券",
                 @"免洗券"];
    [self.view addSubview:self.tableV];
}
-(void)addBtnView//创建按钮
{
    NSArray *btnTitleArr = @[@"未使用",@"已使用",@"已过期"];
    __weak typeof(self)weakSelf = self;
    for ( int i = 0; i< btnTitleArr.count; i++) {

        _btnLab = [[CanLab alloc]initWithFrame:CGRectMake(SCREENW/3*i, 0, SCREENW/3, 45*screenW_6) textAligment:1 font:15*screenW_6 textColor:i==0?@"#31A660":@"#333333" text:btnTitleArr[i]];
        _btnLab.backgroundColor = [UIColor whiteColor];
        _btnLab.tag = i;
        [self.view addSubview:_btnLab];
        
        if (i == 0) {//默认选中0
            _selectLab = _btnLab;
        }
        _btnLab.block = ^(CanLab *lab){//按钮点击事件
            weakSelf.selectLab.textColor = TextBlack;
            lab.textColor = TextGreen;
            weakSelf.selectLab = lab;
            weakSelf.lineLab.frame = CGRectMake(SCREENW/3*lab.tag,44*screenW_6,SCREENW/3,1*screenW_6);
        };
    }
    
    _lineLab = [UILabel new];
    _lineLab.frame = CGRectMake(0,44*screenW_6, SCREENW/3,1*screenW_6);
    _lineLab.backgroundColor = TextGreen;
    [self.view addSubview:_lineLab];
}

//筛选按钮
-(CanLab *)ChooseLab
{
    if (!_ChooseLab) {
        
        _ChooseLab = [[CanLab alloc]initWithFrame:CGRectMake(0,50*screenW_6, SCREENW, 40*screenW_6) textAligment:1 font:15*screenW_6 textColor:@"#333333" text:@"筛选"];
        _ChooseLab.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_ChooseLab];
        __weak typeof(self)weakSelf = self;
        _ChooseLab.block = ^(CanLab *lab){
            weakSelf.isChoose = YES;
            weakSelf.tableV.frame = CGRectMake(0,45*screenW_6, SCREENW, SCREENH-64-45*screenW_6);
            [weakSelf.tableV reloadData];
        };
    }
    return _ChooseLab;
}
-(NSMutableArray *)dataArr//初始化数组
{
    if (!_dataArr) {
        _dataArr = @[@"",
                     @"",
                     @""].mutableCopy;
    }
    return _dataArr;
}
//创建TableView
-(UITableView *)tableV
{
    if (!_tableV) {
        _tableV = [[UITableView alloc] initWithFrame:CGRectMake(0,90*screenW_6, SCREENW, SCREENH-64-90*screenW_6) style:UITableViewStyleGrouped];
        _tableV.backgroundColor = backColor;
        _tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableV.showsVerticalScrollIndicator = NO;
        _tableV.rowHeight = 100*screenW_6;
        _tableV.delegate = self;
        _tableV.dataSource = self;
    }
    return _tableV;
}

#pragma Mark TableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
//行
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (_isChoose) {
        return 3;
    }
    return 3;
}
//区头高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10*screenW_6;
}
//区尾高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (_isChoose && section == 2) {
        return 80*screenW_6;
    }
    return 0.000001;
}
//加载Cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    if (_isChoose) {//筛选
        ChooseKindCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChooseKindCell"];
        if (cell == nil) {
            cell = [[ChooseKindCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ChooseKindCell"];
        }
        [cell loadCellArr:_listArr];
        return cell;
    }else//电子券
    {
        DZQCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DZQCell"];
        if (cell == nil) {
            cell = [[DZQCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"DZQCell"];
        }
        [cell loadCell];
        return cell;
    }
   
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    //筛选时区尾
    if (_isChoose && section == 2) {
        
        UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, 80*screenW_6)];
   
        CanLab * trueLab = [[CanLab alloc]initWithFrame:CGRectMake(SCREENW/3,20*screenW_6, SCREENW/3, 40*screenW_6) textAligment:1 font:15*screenW_6 textColor:@"#ffffff" text:@"筛选"];
        [trueLab.layer setCornerRadius:5*screenW_6];
        trueLab.layer.masksToBounds = YES;
        trueLab.backgroundColor = blueColor;
        [footView addSubview:trueLab];
        __weak typeof(self)weakSelf = self;
        trueLab.block = ^(CanLab *lab){
            weakSelf.isChoose = NO;
            weakSelf.tableV.frame = CGRectMake(0,90*screenW_6, SCREENW, SCREENH-64-90*screenW_6);
            [weakSelf.tableV reloadData];
        };
        return footView;
    }
    return nil;
   
}

@end
